<template>
  <el-form :model="user" :rules="rules" ref="userform" label-width="100px" v-loading="loading" >
    <el-form-item label="登陆帐号" prop="userName">
      <el-input v-model="user.userName" disabled></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="user.name"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phoneNumber">
      <el-input v-model="user.phoneNumber"></el-input>
    </el-form-item>
    <!-- <el-form-item label="电子邮箱" prop="emailAddress">
      <el-input v-model="user.emailAddress"></el-input>
    </el-form-item> -->
    <el-form-item label="开户银行" prop="bank">
      <el-input v-model="user.bank"></el-input>
    </el-form-item>
    <el-form-item label="银行卡号" prop="bankCardNumber">
      <el-input v-model="user.bankCardNumber"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="identityCard">
      <el-input v-model="user.identityCard" :disabled="canEditCardId"></el-input>
    </el-form-item>
    <el-form-item label="联系地址" prop="address">
      <el-input v-model="user.address"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button round type="primary" @click="onSubmit">保存</el-button>
      <el-button round @click="resrtFrom" >重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from 'vuex'
import { Update, GetBaseUserInfo, ValidateIdcard } from '@/api/user'

export default {
  name: 'editUser',
  props: {
    userId: {
      type: Number,
      default: 0
    }
  },
  components: {},
  data() {
    return {
      user: {
        id: 0,
        userName: '',
        password: '',
        password2: '',
        name: '',
        phoneNumber: '',
        emailAddress: '',
        identityCard: '',
        address: '',
        bank: '',
        bankCardNumber: '',
        accountType: 1,
        currentNetPointId: 0
      },
      loading: false,
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        // emailAddress: [
        //   { required: true, message: '请输入电子邮箱', trigger: 'blur' },
        //   { pattern: /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/, message: '请输入正确的电子邮箱', trigger: 'blur' }
        // ],
        bank: [
          { required: true, message: '请输入开户银行', trigger: 'blur' }
        ],
        bankCardNumber: [
          { required: true, message: '请输入银行卡号', trigger: 'blur' }
        ],
        identityCard: [
          { required: true, message: '请输入正确的身份证号', trigger: 'blur' },
          { validator: ValidateIdcard, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ]),
    canEditCardId() {
      return !(this.roles.includes('ADMIN') && this.userId !== 0)
    }
  },
  watch: {
    userId: {
      immediate: true,
      handler(newVal) {
        this.getUserInfo(newVal)
      }
    }
  },
  created() {
    // this.getUserInfo(0)
  },
  methods: {
    getUserInfo(uid) {
      var _this = this
      GetBaseUserInfo({ id: uid }).then(result => {
        _this.user = result.data
      })
    },
    onSubmit() {
      var _this = this
      _this.$refs.userform.validate(valid => {
        if (valid) {
          _this.loading = true
          Update(_this.user).then(result => {
            _this.$message.success('更新成功')
            _this.$emit('updateSuccessed', result)
            _this.loading = false
          }).catch(response => {
            _this.loading = false
          })
        }
      })
    },
    resrtFrom() {
      this.$refs['userform'].resetFields()
    }
  }
}
</script>
